<?php
class Verify extends CI_Controller
{
	public function __construct(){
		parent::__construct();
		$this->load->model("model_user"); // gọi model
	}
	public function index(){ // Login
		$data['title'] = "Login Administrator";
		// Get post : $this->input->post("name");
		$data['errors'] = "";
		if($this->input->post("submit") != ""){ // đã nhấn submit
			$this->form_validation->set_rules('user','Tên đăng nhập', 'required|trim');
			$this->form_validation->set_rules('pass','Mật khẩu', 'required|trim');
			if($this->form_validation->run() == FALSE){
				$this->load->view("login",$data);
			}else{ // Check form true
				$name = $this->input->post("user");
				$pass = $this->input->post("pass");
			  	$info = $this->model_user->checkLog($name,$pass);
				if($info == FALSE){ // Login faild
					$data['errors'] = "Sai username or password!";
					$this->load->view("login",$data);
				}else{ // Login true
					$session = array(
									"ses_id" => $info['user_id'],
									"ses_user" => $info['user_name'],
									"ses_level" => $info['user_level']
								);
					// Lưu session
					$this->session->set_userdata($session);
					redirect(base_url()."admin/index");
					exit();
				}
			}
			
		}else{
			$this->load->view("login",$data);
		}
	}
	public function logout(){
		$this->session->sess_destroy();
		redirect(base_url()."admin/verify");
		exit();
	}
}